// //删除
// function del(){
//     let flag = confirm("确定要删除吗？")
// }
// //使用js给删除按钮添加点击事件。

// 第二种
let node = document.querySelectorAll("tr>td:nth-child(6) input:nth-child(2)");
//获取元素
node.forEach(function (item, i) {
    
    //绑定事件
    item.onclick = function () {
        if (confirm("确定要删除吗？")) {
            item.parentElement.parentElement.remove()
            sum(document.querySelectorAll("tr td:nth-child(4) input"))
        }
    }

    //收藏
    console.log(item.previousElementSibling)
    item.previousElementSibling.onclick = function () {

        if (this.value == "收藏") {
            alert("收藏成功")
            console.log(this)
            this.value = "已收藏"
            this.setAttribute("class", "btn btn-danger btn-xs")
        } else {
            if (confirm("确定要取消收藏吗？")) {
                this.value = "收藏"
                this.setAttribute("class", "btn btn-xs bg-info")
            }
        }
    }


})
//全选功能
let node1 = document.querySelector("#master");
let node2 = document.querySelectorAll("tr>td:nth-child(1) input");

// node1.onclick = function () {
//     if (node1.checked) {
//         for (let i = 0; i < node2.length; i++) {
//             node2[i].checked = true
//         }
//     } else {
//         for (let i = 0; i < node2.length; i++) {
//             node2[i].checked = false
//         }
//     }
// }

node1.onclick = function () {
    for (let i = 0; i < node2.length; i++) {
        node2[i].checked = node1.checked
    }
}
for (var i = 1; i < node2.length; i++) {
    node2[i].onclick = function () {
        choose();
    }
    //反勾选方法
    function choose() {
        let node2 = document.querySelectorAll("tr>td:nth-child(1) input");
        let flag = true;
        for (var i = 1; i < node2.length; i++) {
            if (!node2[i].checked) {
                flag = false;
            }
        }
        if (flag) {
            node1.checked = true;
        } else {
            node1.checked = false;
        }
    }
}
//删除
function dal() {
    let node2 = document.querySelectorAll("tr>td:nth-child(1) input");
    for (var i = 1; i < node2.length; i++) {
        node2[i].onclick = function () {
            let flag = true;
            for (var i = 1; i < node2.length; i++) {
                if (!node2[i].checked) {
                    flag = false;
                }
            }
            if (flag) {
                node1.checked = true;
            } else {
                node1.checked = false;
            }
        }
    }
}
//总价格计算
//获取所有的数量和单价
var numNode = document.querySelectorAll("tr td:nth-child(4) input");
console.log(price)
numNode.forEach(function (item, i) {
    this.onchange = function () {
        //获取数量（this.value)，获取单价，相乘，填充到总价格里
        var numNode1 = document.querySelectorAll("tr td:nth-child(4) input");
        sum(numNode1);
    }
})
sum(numNode); //刚开始的时候计算一次

function sum(numNode) {
    var prices = 0
    numNode.forEach(function (item, i) {
        prices += item.value * item.getAttribute("money")
    })
    document.getElementById("prices").innerText = prices

}
//获取时间
function getTime() {
    let date = new Date();
    let h = date.getHours();
    let m = date.getMinutes();
    let s = date.getSeconds();
    let y = date.getFullYear();
    let M = date.getMonth() + 1;
    let d = date.getDate();
    let m1
    if (m < 10) {
        m1 = 0 + "" + m
    } else {
        m1 = m
    }
    let s1
    if (s < 10) {
        s1 = 0 + "" + s
    } else {
        s1 = s
    }
    let sj = y + "-" + M + "-" + d + "  " + h + ":" + m1 + ":" + s1;
    document.getElementById("time").innerText = sj
}
//定时函数
// let myinterval = setInterval('getTime()', 1000)
// console.log(myinterval) //定时器编号
//延时函数
// setTimeout(function () {
//     alert("欢迎登陆")
// }, 1000)

//设置点击暂停时间和开始
let time = document.getElementById("time")
let index = null

function start() {
    if (index == null) {
        index = setInterval('getTime()', 1000)
        time.setAttribute("title", "时间运行")
    }
}

function stop() {
    clearInterval(index)
    index = null
    time.setAttribute("title", "时间停止")
}
start()
let j = 1
time.onclick = function () {
    if (j % 2 == 1) {
        stop()
    } else {
        start()
    }
    j++
}
//结算
function show(){
    let name = document.querySelectorAll("tr td:nth-child(3)");
    console.log(name)
    let last = document.getElementById("last");
    last.innerHTML = "<h4>您的购买信息是：</h4><h4></h4><h4></h4>"
    let str = null;
    for(let i =1;i<name.length;i++){
        var next1 = name[i].nextElementSibling.firstElementChild.value;
        var next2 = name[i].nextElementSibling.firstElementChild.getAttribute("money");
        var index = last.firstElementChild.nextElementSibling ;
        str  += "书名是："+name[i].innerText+"  "+"数量:"+next1+"  "+"总价："+next1*next2+"<br>";
    }       
    last.firstElementChild.nextElementSibling.innerText = str;
}
//添加书名
function createtr(){
    //获取表单数据
    let bookid = document.getElementById("id").value
    let bookname = document.getElementById("name").value
    let bookprice = document.getElementById("price").value

    // //拼出一个tr ,然后追加到table里
    // let tr = document.createElement("tr");
    // let td1 = document.createElement("td")
    // td1.innerHTML = '<input type="checkbox" />';
    // let td2 = document.createElement("td");
    // td2.innerText = bookid;
    // let td3 = document.createElement("td");
    // td3.innerText = bookname;
    // let td4 = document.createElement("td");
    // td4.innerHTML = '<input type="number" value="1" min="1" money="'+bookprice+'"/>'
    // let td5 = document.createElement("td");
    // td5.innerHTML = '<span>￥</span>'+bookprice;
    // let td6 = document.createElement("td");
    // td6.innerHTML = '<input type="button" value="收藏" class="btn btn-xs bg-info" /><input type="button" value="删除" class="btn btn-warning btn-xs" onclick="dal()"/>';
    // tr.appendChild(td1)
    // tr.appendChild(td2)
    // tr.appendChild(td3)
    // tr.appendChild(td4)
    // tr.appendChild(td5)
    // tr.appendChild(td6)

    let str = '<tr>'+
                 '<td><input type="checkbox" /></td>'+
                 '<td>'+bookid+'</td>'+
                 '<td>'+bookname+'</td>'+
                 '<td><input type="number" value="1" min="1" money="'+bookprice+'"/></td>'+
                 '<td><span>￥</span>'+bookprice+'</td>'+
                 '<td>'+
                    '<input type="button" value="收藏" class="btn btn-xs bg-info" />'+
                    ' <input type="button" value="删除" class="btn btn-warning btn-xs"  onclick="dal()"/>'+
              '</td>';
    let tr = document.createElement("tr")//创建一个空tr
    tr.innerHTML = str;
    document.getElementById("tbody").appendChild(tr)
//重新计算
    sum(document.querySelectorAll("tr td:nth-child(4) input"))

}